/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui;

import com.anhndt.audiobook.business.ListLoaderTask;
import com.anhndt.audiobook.entity.ItemEntity;
import com.anhndt.audiobook.iface.ProcessListener;
import com.anhndt.audiobook.utils.StringUtil;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import java.util.Vector;

/**
 *
 * @author Sun
 */
public class ContentForm extends BaseForm {

    public ContentForm(BaseForm previous, ItemEntity content) {
        super(null, previous, content);
        init();
        try {
            form.setTitle(content.name);
            splitContent(content.description);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void init() {
        Display.getInstance().setThirdSoftButton(false);
        super.init();
        form.setUIID("ContentForm");
        form.setScrollableX(false);
        form.setScrollableY(true);
    }

    protected void backScreen() {
        parentEntity.removeAllChilrends();
        parentEntity = null;
        super.backScreen();
    }

    protected void addCommand() {
        //form.addCommand(cmdMainScreen);
    }

    public void actionPerformed(ActionEvent evt) {
    }

    String getUrlImage(String url) {
        return Util.getImageUrl(url, Display.getInstance().getDisplayWidth() - 20, Display.getInstance().getDisplayHeight() * 2/ 3, false);
    }

    void splitContent(String content) {
        System.out.println("[MessageDetailForm] content = " + content);
        if (StringUtil.isNullOrEmpty(content)) {
            return;
        }
        final String TAG_IMAGE = "<img";
        final String HTTP = "http";
        final String END_TAG = "/>";
        Vector urlImages = new Vector();
        Vector images = new Vector();

        int indexTagImage = content.indexOf(TAG_IMAGE);
        int indexEndTagImage;
        int indexHttp;
        String text;
        String urlImage = null;
        int indexQuoth;///vi tri dau ngoac kep
        //neu trong doan text content ko co url image
        //thi day tat ca text vao form;
        body.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        if (indexTagImage == -1) {
            body.addComponent(createTextArea(content));
        }
        while (indexTagImage != -1) {
            if (indexTagImage > 0) {
                text = content.substring(0, indexTagImage);
                body.addComponent(createTextArea(text));
            }
            indexEndTagImage = content.indexOf(END_TAG);
            try {
                indexHttp = content.indexOf(HTTP);
                urlImage = content.substring(indexHttp, indexEndTagImage);
                indexQuoth = urlImage.indexOf("\"");
                urlImage = getUrlImage(urlImage.substring(0, indexQuoth));
            } catch (Exception e) {
                e.printStackTrace();
            }
            content = content.substring(indexEndTagImage + END_TAG.length());
            if (urlImage != null && urlImage.trim().length() > 0) {
                Label lblImage = new Label();
                lblImage.setAlignment(Label.CENTER);
                body.addComponent(lblImage);
                images.addElement(lblImage);
                urlImages.addElement(urlImage);
            }
            indexTagImage = content.indexOf(TAG_IMAGE);
            if (indexTagImage == -1) {
                content.trim();
                if (content.length() > 0) {
                    body.addComponent(createTextArea(content));
                }
            }
        }
        if (urlImages != null && urlImages.size() > 0) {
            loadImage(urlImages, images);
        }
    }

    void loadImage(final Vector urlImages, final Vector images) {
        ProcessListener listener = new ProcessListener() {

            public void onSuccess(Object result, int index) {
                try {
                    byte[] data = (byte[]) result;
                    //load image into thumb
                    Image icon = Image.createImage(data, 0, data.length);
                    ((Label) images.elementAt(index)).setIcon(icon);
                    form.repaint();
                } catch (Exception e) {
                }
            }

            public void onError(String status, int index) {
            }
        };
        ListLoaderTask loader = new ListLoaderTask(StringUtil.convertVectorToString(urlImages), listener);
        loader.start();
    }

    TextArea createTextArea(String text) {
        TextArea txt = new TextArea(text);
        txt.setUIID("ContentText");
        txt.setFocusable(false);
        txt.setEditable(false);
        return txt;
    }
}
